import logging
from django.core.signals import got_request_exception

from django.dispatch import receiver

# 缓存信号
from django.db.models.signals import post_save, post_delete
from django.core.cache import cache

from .models import *

logger = logging.getLogger('django.request')


# 日志信号监听
@receiver(got_request_exception)
def log_exception(sender, request, **kwargs):
    logger.error(f"Exception occurred : {request.path}", exc_info=True)


# 缓存信号监听
@receiver(post_delete, sender=UserProfile)
@receiver(post_save, sender=UserProfile)
def clear_cache(sender, **kwargs):
    cache_key = 'app_key'
    cache.delete(cache_key)
